package com.ruoyi.web.controller.bbsAdmin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.bbsAdmin.domain.School;
import com.ruoyi.bbsAdmin.domain.UserSchool;
import com.ruoyi.bbsAdmin.domain.vo.bbs_userVo;
import com.ruoyi.bbsAdmin.mapper.UserSchoolMapper;
import com.ruoyi.bbsAdmin.service.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bbsAdmin.domain.bbs_user;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.bbsAdmin.domain.vo.judgeStructure;
import com.ruoyi.bbsAdmin.domain.vo.multiUserStructure;
import com.ruoyi.bbsAdmin.domain.vo.updateUserStructure;
/**
 * 论坛用户Controller
 * 
 * @author ruoyi
 * @date 2022-03-21
 */
@RestController
@RequestMapping("/bbs_user/bbs_user")
public class bbs_userController extends BaseController
{
    @Autowired
    private Ibbs_userService bbs_userService;

    @Autowired
    private ISchoolService iSchoolService;


    @Resource
    private com.ruoyi.bbsAdmin.mapper.UserSchoolMapper UserSchoolMapper;

    @Autowired
    RedisService redisService;


    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @GetMapping("/schoolList")
    public AjaxResult getSchoolList(){
        List<School> schools = iSchoolService.selectSchoolListByRole(new School());
        //过滤学校，只保留状态为0或者6的学校
        List<School> filteredSchools = schools.stream()
//                .filter(school -> (school.getState() == 0 || school.getState() == 6 || school.getState() == 2) && school.getSchoolid()!=1)
                .filter(school -> school.getState() == 0 || school.getState() == 6 )
                .collect(Collectors.toList());
        return AjaxResult.success(filteredSchools);
    }

    /**
     * 查询论坛用户列表
     */
    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @PostMapping("/list")
//    RequestParam
    public TableDataInfo list(multiUserStructure multiUserStructure)
    {
        //startPage();  startPage只对他下面第一句查询sql有用
        List<bbs_user> list = bbs_userService.selectbbs_userList(multiUserStructure);
        if(list!=null){
            System.out.println("成员列表的个数是" + list.size());
            return getDataTable(list);
        }else{
            return null;
        }

    }

    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @GetMapping("/list")
//    RequestParam
    public TableDataInfo list2(multiUserStructure multiUserStructure)
    {
        Long schoolId = multiUserStructure.getSchoolid();
//        Long schoolId = null;
        startPage();
        List<bbs_user> list = bbs_userService.selectbbs_userList(multiUserStructure);
        if(list!=null){
            return getDataTable(list);
        }else{
            return null;
        }

    }

    /**
     * 查询论坛机器人用户列表
     */
    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @GetMapping("/robotList")
//    RequestParam
    public TableDataInfo robotList(bbs_userVo bbs_userVo)
    {
        startPage();
        List<bbs_userVo> list = bbs_userService.selectbbs_robotList(bbs_userVo);
        return getDataTable(list);

    }

    /**
     * 导出论坛用户列表
     */
    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @Log(title = "论坛用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, bbs_user bbs_user)
    {
        Long schoolId = 1L;
        List<bbs_user> list = bbs_userService.selectbbs_userListExport(schoolId,bbs_user);
        ExcelUtil<bbs_user> util = new ExcelUtil<bbs_user>(bbs_user.class);
        util.exportExcel(response, list, "论坛用户数据");
    }

    /**
     * 获取论坛用户详细信息
     */
    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @GetMapping(value = "/{userid}")
    public AjaxResult getInfo(@PathVariable("userid") String userid)
    {
        return AjaxResult.success(bbs_userService.selectbbs_userByUserid(userid));
    }

    /**
     * 新增论坛用户
     */
    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @Log(title = "论坛用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody bbs_userVo Bbs_userVo)
    {
        return toAjax(bbs_userService.insertbbs_user(Bbs_userVo));
    }

    /**
     * 修改论坛用户
     */
    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @Log(title = "论坛用户", businessType = BusinessType.UPDATE)
    @PutMapping
//    public AjaxResult edit(@RequestBody bbs_user bbs_user)
//    {
//        return toAjax(bbs_userService.updatebbs_user(bbs_user));
//    }


    //更改结构体即可，传递三个值，分别是用户id，用户状态以及用户类型
    public AjaxResult edit(@RequestBody updateUserStructure updateUserStructure)
    {
        //方法重写
        return toAjax(bbs_userService.updateUser(updateUserStructure));
    }

    /**
     * 删除论坛用户
     */
    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @Log(title = "论坛用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userids}")
    public AjaxResult remove(@PathVariable String[] userids)
    {
        return toAjax(bbs_userService.deletebbs_userByUserids(userids));
    }

    /**
     * 审核论坛用户
     */
    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @PostMapping("/judge")

    public Integer judge(judgeStructure judgeStructure)
    {
        String userId = judgeStructure.getUserid();
        Integer state = judgeStructure.getState();
        Long schoolid =  judgeStructure.getSchoolId();
        Integer res = bbs_userService.judgebbs_userByUserid(userId,state,schoolid);
        return res;
    }



    @ApiOperation("获取用户在对应学校下的状态")
    @GetMapping("/getUserSchool")
    //根据用户ID获取用户信息
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true),
            @ApiImplicitParam(name = "schoolId", value = "学校ID", required = true)
    })
    public AjaxResult getUserSchool(String userId, Integer schoolId)
    {
        //更新校园信息
        QueryWrapper<UserSchool> UserSchoolQueryWrapper = new QueryWrapper<>();
        UserSchoolQueryWrapper.eq("userId",userId);
        UserSchoolQueryWrapper.eq("schoolId",schoolId);
        List<UserSchool>  recordList = UserSchoolMapper.selectList(UserSchoolQueryWrapper);
        return AjaxResult.success(recordList.get(0));
    }

    /**
     * 修改论坛用户官方认证状态
     */
    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @Log(title = "论坛用户", businessType = BusinessType.UPDATE)
    @PutMapping("/changeUserState")
    public AjaxResult changeUserState(@RequestBody bbs_user bbs_user)
    {
        int result = bbs_userService.updatebbs_user(bbs_user);
        //修改redis中的认证状态
        Map<String, String> updateInfoMap = new HashMap<>();
        updateInfoMap.put("userState",String.valueOf(bbs_user.getUserstate()));
        int redisRes =redisService.updateSomeUserInfoInRedis(bbs_user.getUserid(),updateInfoMap);
        return toAjax(result);
    }

    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @GetMapping("/allSchoolUserList")
//    RequestParam
    public TableDataInfo allSchoolUserList(multiUserStructure multiUserStructure)
    {
        //startPage();  startPage只对他下面第一句查询sql有用
        List<bbs_user> list = bbs_userService.selectAllSchoolUserList(multiUserStructure);
        if(list!=null){
            System.out.println("成员列表的个数是" + list.size());
            return getDataTable(list);
        }else{
            return null;
        }

    }
}
